<template>
  <div class="app-container">
    <el-card class="box-card">
      <template #header>
        <div class="clearfix">
          <span>{{ isEdit ? '编辑院系' : '新增院系' }}</span>
        </div>
      </template>

      <el-form
        ref="departmentFormRef"
        :model="form"
        :rules="rules"
        label-width="100px"
        style="max-width: 600px"
      >
        <el-form-item label="院系名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入院系名称" />
        </el-form-item>
        
        <el-form-item label="院系描述" prop="description">
          <el-input
            v-model="form.description"
            type="textarea"
            placeholder="请输入院系描述"
          />
        </el-form-item>
        
        <el-form-item label="院长" prop="dean">
          <el-input v-model="form.dean" placeholder="请输入院长姓名" />
        </el-form-item>
        
        <el-form-item label="联系方式" prop="contact">
          <el-input v-model="form.contact" placeholder="请输入联系方式" />
        </el-form-item>
        
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" @click="submitForm">保存</el-button>
          <el-button @click="goBack">返回</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { getDepartmentById, createDepartment, updateDepartment } from '@/api/edu'

// 路由相关
const route = useRoute()
const router = useRouter()

// 表单引用
const departmentFormRef = ref()

// 是否为编辑模式
const isEdit = ref(false)

// 表单数据
const form = reactive({
  id: undefined,
  name: '',
  description: '',
  dean: '',
  contact: '',
  status: 1
})

// 表单验证规则
const rules = {
  name: [{ required: true, message: '请输入院系名称', trigger: 'blur' }]
}

// 获取院系详情
const getDepartmentDetail = async (id) => {
  try {
    const response = await getDepartmentById(id)
    Object.assign(form, response.data)
  } catch (error) {
    ElMessage.error('获取院系信息失败')
  }
}

// 提交表单
const submitForm = async () => {
  if (!departmentFormRef.value) return
  
  await departmentFormRef.value.validate((valid) => {
    if (valid) {
      if (isEdit.value) {
        // 编辑模式
        updateDepartment(form)
          .then(() => {
            ElMessage.success('保存成功')
            goBack()
          })
          .catch(() => {
            ElMessage.error('保存失败')
          })
      } else {
        // 新增模式
        createDepartment(form)
          .then(() => {
            ElMessage.success('保存成功')
            goBack()
          })
          .catch(() => {
            ElMessage.error('保存失败')
          })
      }
    }
  })
}

// 返回上一页
const goBack = () => {
  router.push('/edu/departments')
}

// 初始化
onMounted(() => {
  // 判断是否为编辑模式
  if (route.params.id) {
    isEdit.value = true
    getDepartmentDetail(route.params.id)
  }
})
</script>

<style scoped>
.app-container {
  padding: 20px;
}
</style>